package com.bwie.controller;

import cn.hutool.core.util.RandomUtil;
import com.bwie.pojo.TbUser;
import com.bwie.service.TbUserService;
import com.bwie.utils.KaptchaUtils;
import com.bwie.utils.R;
import com.bwie.vo.LoginInfoVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.concurrent.TimeUnit;

@RestController
@RequestMapping("/user/index")

public class IndexController {
    @Autowired
    TbUserService tbUserService;
    @Autowired
    StringRedisTemplate stringRedisTemplate;

    /**
     * 登录接口
     * @param loginInfoVo
     * @return
     */
    @PostMapping("/login")
    public R login(@RequestBody LoginInfoVo loginInfoVo){
        return tbUserService.login(loginInfoVo);
    }


    /**
     * 生成图片验证码
     * @param httpServletResponse
     */
    @GetMapping("/code")
    public void code(HttpServletResponse httpServletResponse){
        String code = RandomUtil.randomNumbers(4);
        String key = code;
        stringRedisTemplate.opsForValue().set(key,code,5, TimeUnit.MINUTES);
        KaptchaUtils.generateCaptcha(httpServletResponse,code);

    }


    /**
     * 手机验证码
     * @param code
     * @param mobile
     * @return
     */
    @PostMapping("/sendCode/{code}/{mobile}")
    public R sendCode(@PathVariable("code")String code,@PathVariable("mobile")String mobile){
        return tbUserService.sendCode(code,mobile);
    }


    /**
     * 注册用户接口
     * @param tbUser
     * @return
     */
    @PostMapping("/addUser1")
    public R addUser(@RequestBody TbUser tbUser){
        return tbUserService.addUser(tbUser);
    }

    @PostMapping("/updateUser")
    public R updateUser(@RequestBody TbUser tbUser){
        return tbUserService.updateUser(tbUser);
    }




}
